package com.sailfish.springbootdemo.service.db2.impl;

import com.alibaba.fastjson.JSONObject;
import com.sailfish.springbootdemo.configs.UserHeaderHolder;
import com.sailfish.springbootdemo.dao.db2.PcbConfigDao;
import com.sailfish.springbootdemo.pojo.Result;
import com.sailfish.springbootdemo.pojo.db2.PcbConfig;
import com.sailfish.springbootdemo.pojo.db4.Operate;
import com.sailfish.springbootdemo.service.db2.PcbConfigService;
import com.sailfish.springbootdemo.service.db4.OperateService;
import com.sailfish.springbootdemo.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.Date;
import java.util.List;

@Service
public class PcbConfigServiceImpl implements PcbConfigService {

    @Autowired
    PcbConfigDao pcbConfigDao;
    @Autowired
    OperateService operateService;

    @Override
    public List<PcbConfig> getAll(String attrName, String attrValue, String remark, Integer status) {
        return pcbConfigDao.getAllByLimit(attrName, attrValue, remark, status);
    }

    @Override
    public List<PcbConfig> getBulletinCfg() {
        return pcbConfigDao.getBulletins("pcbMaterialBulletin");
    }

    @Transactional(transactionManager = "MysqlTransactionManager2")
    @Override
    public Result insert(PcbConfig param) {
        try {
            List<PcbConfig> items = pcbConfigDao.getAllByLimit(param.getAttrName(), param.getAttrValue(), param.getRemark(), param.getStatus());
            if (items.stream().count() > 0) {
                return ResultUtil.error(500, "pcb配置已存在", "pcb config exist", null);
            } else {
                int count = pcbConfigDao.insert(param);
                if (count > 0) {
                    String userId = UserHeaderHolder.getUserId();
                    operateService.addOperate(new Operate(2, Integer.parseInt(userId), "PCB配置数据", 1, 1, new Date(), null, JSONObject.toJSONString(param)));
                    return ResultUtil.success(param);
                }
                return ResultUtil.error(500, "操作失败", "Operate Fail", null);
            }

        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }

    @Transactional(transactionManager = "MysqlTransactionManager2")
    @Override
    public Result delete(Integer id) {
        try {
            PcbConfig oldData = pcbConfigDao.getById(id);
            int count = pcbConfigDao.delete(id);
            if (count > 0) {
                String userId = UserHeaderHolder.getUserId();
                operateService.addOperate(new Operate(2, Integer.parseInt(userId), "PCB配置数据", 2, 1, new Date(), JSONObject.toJSONString(oldData), null));
                return ResultUtil.success(null);
            }
            return ResultUtil.error(500, "操作失败", "Operate Fail", null);
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }

    @Transactional(transactionManager = "MysqlTransactionManager2")
    @Override
    public Result update(PcbConfig param) {
        try {
            PcbConfig oldData = pcbConfigDao.getById(param.getId());
            int count = pcbConfigDao.update(param);
            if (count > 0) {
                String userId = UserHeaderHolder.getUserId();
                operateService.addOperate(new Operate(2, Integer.parseInt(userId), "PCB配置数据", 3, 1, new Date(), JSONObject.toJSONString(oldData), JSONObject.toJSONString(param)));
                return ResultUtil.success(null);
            }
            return ResultUtil.error(500, "操作失败", "Operate Fail", null);
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }
}
